# -*- coding: iso-8859-2 -*-

import os
import logging
import traceback
import re
import string

from authorshipanalyzer.textmodifier import Editor
from authorshipanalyzer.message import Message

class CharacterFilter(Editor):
    '''
    Class filtering email address from unexpected characters
    e.g. (login@domain.com) -> login@domain.com
    e.g. "Zyzio Marzyciel" -> Zyzio Marzyciel
    '''
    
    def editMessage(self, message):
        address = self._removeNonestandardChars(message.getAddress())
        if address.strip() == "":
            return False
        message.setAddress(address) 
        
        author = self._removeNonestandardChars(message.getAuthor())
        if author.strip() == "":
            return False        
        message.setAuthor(author)
        return True

        
    def _removeNonestandardChars(self, text):
        '''
        Remove puctuations apart from '.', '@', '_'
        '''
        for punctuation in string.punctuation:
            if punctuation not in ('.', '@', '_'):
               text = text.replace(punctuation, '')
               
        return text
        
    def __str__(self):
        return __name__   
     
if __name__ == "__main__":
     print "Remove:", string.punctuation 
     
     a = CharacterFilter()
     message = Message()
     message.setAddress("<arturcz@cyf.gov.pl>")
     message.setAuthor("(Janek)")
     a.editMessage(message)   
     print message.getAddress()